import { Controller } from 'egg';

interface resultType {
  code: number;
  message: string;
  data: any[]
}
export default class LoginController extends Controller {

  // 定义注册方法传入的参数校验
  private registerRule = {
    username: 'string',
    account: 'string',
    password: 'string',
    sex: 'string',
    age: 'int',
  };
  /**
   * 注册
   * 接收参数
   * username 用户名
   * account 账户
   * password 密码
   * age 年龄
   * sex 性别
   */
  public async register() {
    const { ctx } = this;
    ctx.validate(this.registerRule, ctx.request.body);
    const result:resultType = await ctx.service.loginService.register(ctx.request.body) as resultType;
    this.ctx.body = result;
  }

  /**
   * 登录
   * 接收参数
   * account 账户
   * password 密码
   */
  public async login() {
    const { ctx } = this;
    ctx.validate({ account: 'string', password: 'string' }, ctx.request.body);
    const result:resultType = await ctx.service.loginService.login(ctx.request.body) as resultType;
    this.ctx.body = result;
  }

  /**
   * 修改密码
   * 接收参数
   * account 账户
   * password 密码
   * newPassword 新密码
   */
  public async editPassword() {
    const { ctx } = this;
    ctx.validate({ account: 'string', password: 'string', newPassword: 'string' }, ctx.request.body);
    const result:resultType = await ctx.service.loginService.editPassword(ctx.request.body) as resultType;
    this.ctx.body = result;
  }

}
